package com.dk.service.serviceImpl;

import com.dk.mapper.ChatRecordMapper;
import com.dk.mapper.UserMapper;
import com.dk.pojo.ChatRecord;
import com.dk.service.ChatService;
import com.dk.ws.bean.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    private ChatRecordMapper chatRecordMapper;

    public List<Message> getChatRecord(String sourceId,String destinationId){
        List<ChatRecord> chatRecords=new ArrayList<>();
        if(checkSessionContain(sourceId, destinationId)){
            //检查deleteState状态
            int deleteState = chatRecordMapper.queryChatRecordDeleteState(sourceId, destinationId);
            //source用户没有删除信息
            if(deleteState==0){
                chatRecords=chatRecordMapper.queryBothChatRecord(sourceId, destinationId);
            }
        }
        return parseChatRecordToMessage(sourceId,chatRecords);
    }

    public void saveChatRecord(String sourceId, String destinationId, String message, Timestamp time, int sendState){
        ChatRecord chatRecord=new ChatRecord();
        chatRecord.setSourceId(sourceId);
        chatRecord.setDestinationId(destinationId);
        chatRecord.setMessage(message);
        chatRecord.setTime(time);
        chatRecord.setSendState(sendState);
        chatRecord.setDeleteState(0);
        chatRecordMapper.saveChatRecord(chatRecord);
    }

    public void deleteChatRecord(String sourceId,String destinationId){
        if(checkSessionContain(sourceId, destinationId)){
            //获取对方是否删除信息
            int deleteState= chatRecordMapper.queryChatRecordDeleteState(destinationId,sourceId);
            //互删后执行删除
            if(deleteState==1){
                chatRecordMapper.deleteChatRecord(sourceId, destinationId);
            }
            //对方没有选择删除信息 将继续保留信息 改变deleteState=1
            else if(deleteState==0){
                chatRecordMapper.updateDeleteState(sourceId,destinationId,1);
            }
        }
    }

    public void updateOfflineMessage(String sourceId){
        chatRecordMapper.updateOfflineChatRecord(sourceId);
    }

    public boolean checkHaseOfflineMessage(String sourceId){
        return chatRecordMapper.queryChatRecordSendState(sourceId)>=1?true:false;
    }

    /**
     * 返回Map<uid,List<Message>>
     * @param sourceId
     * @return
     */
    public List<Message> getOfflineMessage(String sourceId){
        List<Message> offlineMessages=new ArrayList<>();
        if(checkHaseOfflineMessage(sourceId)){
            List<ChatRecord> offlineChatRecord=chatRecordMapper.queryOfflineChatRecord(sourceId);
            for(ChatRecord chatRecord:offlineChatRecord){
                String id=chatRecord.getSourceId();
                Message message = new Message();
                message.setSourceId(id);
                message.setMessage(chatRecord.getMessage());
                message.setTime(chatRecord.getTime());
                message.setWho(1);
                offlineMessages.add(message);
            }
        }
        return offlineMessages;
    }

    public List<String> getChatPeople(String sourceId){
        return chatRecordMapper.queryChatPeople(sourceId);
    }


    private boolean checkSessionContain(String sId,String dId){
        return chatRecordMapper.querySessionContain(sId, dId)==0?false:true;
    }

    private List<Message> parseChatRecordToMessage(String sourceId,List<ChatRecord> chatRecords){
        List<Message> messages=new ArrayList<>();
        for(ChatRecord chatRecord:chatRecords){
            Message message=new Message();
            message.setSourceId(sourceId);
            message.setMessage(chatRecord.getMessage());
            message.setTime(chatRecord.getTime());
            String destinationId=chatRecord.getDestinationId();
            //目标用户与源用户一致说明是对方的信息 who=1
            if(sourceId.equals(destinationId)){
                message.setWho(1);
            }else message.setWho(0);

            messages.add(message);
        }
        return messages;
    }
}
